#include <mTask.h>
mTaskNodeInt_t mTaskTreeeCreateNodeInt(int size)
{
    mTaskNodeInt_t node;
    int slen = sizeof(struct mTaskNodeInt) + size;
    node = (mTaskNodeInt_t)malloc(slen);
    if(node!=NULL)  
        memset(node,0,slen);
    return node;
}
mTaskNodeInt_t mTaskTreeSearchInt(struct rb_root *root, size_t key)
{
  	struct rb_node *node = root->rb_node;
  	while (node) {
  		mTaskNodeInt_t data = container_of(node, struct mTaskNodeInt, node);
        
        if(key > data->key)
        {
            node = node->rb_right;
        }
        else if(key < data->key)
        {
            node = node->rb_left;
        }
        else{
            return data;
        }
	}
	return NULL;
}
mTaskNodeInt_t mTaskTreeGetMinNodeInt(struct rb_root *root)
{
    return (mTaskNodeInt_t)rb_first(root);
}
mTaskNodeInt_t mTaskTreeGetMaxNodeInt(struct rb_root *root)
{
    return (mTaskNodeInt_t)rb_last(root);
}
int mTaskTreeInsertInt(struct rb_root *root, mTaskNodeInt_t data)
{
  	struct rb_node **node = &(root->rb_node), *parent = NULL;

  	/* Figure out where to put node node */
  	while (*node) {
  		mTaskNodeInt_t this = container_of(*node, struct mTaskNodeInt, node);
		parent = *node;
        if(data->key > this->key)
        {
            node = &((*node)->rb_right);
        }
        else// if(data->key < this->key)
        {
            node = &((*node)->rb_left);
        }
        //else{
            //return 0;
        //}
  	}
  	/* Add new node and rebalance tree. */
  	rb_link_node(&data->node, parent, node);
  	rb_insert_color(&data->node, root);

	return 1;
}
void mTaskTreeDeleteInt(struct rb_root *root,mTaskNodeInt_t node)
{
	if (node != NULL) {
        rb_erase(&node->node, root);
		free(node);
		node = NULL;
	}
}


mTaskNodeStr_t mTaskTreeeCreateNodeStr(int size)
{
    mTaskNodeStr_t node;
    int slen = sizeof(struct mTaskNodeInt) + size;
    node = (mTaskNodeStr_t)malloc(slen);
    if(node!=NULL)  
        memset(node,0,slen);
    return node;
}
mTaskNodeStr_t mTaskTreeSearchStr(struct rb_root *root, char *key)
{
  	struct rb_node *node = root->rb_node;
  	while (node) {
  		mTaskNodeStr_t data = container_of(node, struct mTaskNodeStr, node);
        int result = strcmp(key, data->key);
		if (result < 0)
  			node = node->rb_left;
		else if (result > 0)
  			node = node->rb_right;
		else
  			return data;
	}
	return NULL;
}
mTaskNodeStr_t mTaskTreeGetMinNodeStr(struct rb_root *root)
{
    return (mTaskNodeStr_t)rb_first(root);
}
mTaskNodeStr_t mTaskTreeGetMaxNodeStr(struct rb_root *root)
{
    return (mTaskNodeStr_t)rb_last(root);
}
int mTaskTreeInsertStr(struct rb_root *root, mTaskNodeStr_t data)
{
  	struct rb_node **node = &(root->rb_node), *parent = NULL;

  	/* Figure out where to put new node */
  	while (*node) {
  		mTaskNodeStr_t this = container_of(*node, struct mTaskNodeStr, node);
  		int result = strcmp(data->key, this->key);
		parent = *node;
  		if (result <= 0)
  			node = &((*node)->rb_left);
  		else// if (result > 0)
  			node = &((*node)->rb_right);
  		//else
  		//	return 0;
  	}
  	/* Add new node and rebalance tree. */
  	rb_link_node(&data->node, parent, node);
  	rb_insert_color(&data->node, root);

	return 1;
}
void mTaskTreeDeleteStr(struct rb_root *root,mTaskNodeStr_t node)
{
	if (node != NULL) {
        rb_erase(&node->node, root);
		free(node);
		node = NULL;
	}
}


